package com.tsinghuabigdata.edu.ddmath.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.constant.AppConst;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.PreferencesUtils;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends Activity implements View.OnClickListener, ViewPager
        .OnPageChangeListener {

    private ViewPager vp_pager;
    private LinearLayout ll_page_1;
    private LinearLayout ll_page_2;
    private LinearLayout ll_page_3;
    private FrameLayout ll_page_4;
    private ImageView iv_roger;
    private ImageView iv_indicator;
    private List<View> views = new ArrayList<>(4);
    private int[] indicatorArr = new int[] {
//            R.drawable.dots_1,
//            R.drawable.dots_2,
//            R.drawable.dots_3,
//            R.drawable.dots_4
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        initView();
    }

    private void initView() {
        vp_pager = (ViewPager) findViewById(R.id.vp_pager);
        ll_page_1 = (LinearLayout) findViewById(R.id.ll_page_1);
        ll_page_2 = (LinearLayout) findViewById(R.id.ll_page_2);
        ll_page_3 = (LinearLayout) findViewById(R.id.ll_page_3);
        ll_page_4 = (FrameLayout) findViewById(R.id.ll_page_4);
        iv_roger = (ImageView) findViewById(R.id.iv_roger);
        iv_indicator = (ImageView) findViewById(R.id.iv_indicator);

        iv_roger.setOnClickListener(this);

        // 初始化子视图
        initPageViews();

        // 设置适配器
        setPageAdapter();

        vp_pager.addOnPageChangeListener(this);
    }

    private void setPageAdapter() {
        vp_pager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public View instantiateItem(ViewGroup container, final int position) {
                View view = views.get(position);
                container.addView(view);
                return view;
            }
        });
    }

    private void initPageViews() {
        ((ViewGroup) ll_page_1.getParent()).removeView(ll_page_1);
        ((ViewGroup) ll_page_2.getParent()).removeView(ll_page_2);
        ((ViewGroup) ll_page_3.getParent()).removeView(ll_page_3);
        ((ViewGroup) ll_page_4.getParent()).removeView(ll_page_4);
        views.add(ll_page_1);
        views.add(ll_page_2);
        views.add(ll_page_3);
        views.add(ll_page_4);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 禁止用户按返回键退出
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        PreferencesUtils.putInt(this, AppConst.WELCOME_OPENED, 1);
        Intent intent = new Intent();
        if (AccountUtils.getLoginUser() != null) {
            intent.setClass(WelcomeActivity.this, MainActivity.class);
        } else {
//            intent.setClass(WelcomeActivity.this, LoginActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        iv_indicator.setImageResource(indicatorArr[position]);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
